using System;
using System.Web.UI.WebControls;

using Impact.Controllers;
using Impact.Model.Objects;
using Impact.Web.Foundation.Controls;

namespace Impact.Web.Admin
{
    public partial class Profiles : Impact.Web.Foundation.PageBase
    {

        /// <summary>
        /// loading event handler
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad( EventArgs e )
        {
            base.OnLoad( e );


            if ( Request.Form["__EVENTTARGET"] == "Bind" )
            {
                Bind();
            }

        }


        /// <summary>
        /// overridden to handling binding on this page
        /// </summary>
        protected override void Bind()
        {
            ProfileCollection profiles = ProfileController.GetProfiles();
            if ( profiles.Count > 0 )
            {
                profileList.DataSource = profiles;
                profileList.DataBind();
            }
            else
            {
                profileList.Visible = false;
                noProfilesPanel.Visible = true;
            }
        }

        /// <summary>
        /// event handler for when a button is clicked in the profile listing repeater
        /// </summary>
        protected void ProfileList_ItemCommand( object source, RepeaterCommandEventArgs e )
        {
            int profileId = Int32.Parse( e.CommandArgument.ToString() );
            switch ( e.CommandName )
            {
                case "delete":
                    ProfileController.DeleteProfile( profileId );
                    MessageBox.Show( string.Format( "Retailer deleted.", profileId ) );
                    break;

                case "toggle":
                    ProfileController.ToggleEnabled( profileId, this.UserId );
                    //MessageBox.Show( string.Format( "Profile #{0} toggled.", profileId ) );
                    break;

                default:
                    throw new ArgumentException( string.Format( "Command not found: {0}", e.CommandName ) );
            }
            Bind();
        }

    }
}
